<!DOCTYPE HTML>
<html>
<head>
    <title>购物车</title>
    <link href="/static/myhome/css/bootstrap.css" rel='stylesheet' type='text/css'/>
    <script src="/static/myhome/js/jquery.min.js"></script>
    <script src="/static/myhome/js/bootstrap.min.js"></script>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="/static/myhome/js/jquery.min.js"></script>
    <!-- Custom Theme files -->
    <link href="/static/myhome/css/style.css" rel='stylesheet' type='text/css'/>
    <!-- Custom Theme files -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="application/x-javascript">
         addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); }
    </script>
    </script>
<!----webfonts---->
<link href='http://fonts.googleapis.com/css?family=Exo+2:100,200,300,400,500,600,700,800,900' rel='stylesheet' type='text/css'>
<!-- Add fancyBox main JS and CSS files -->
<script src="/static/myhome/js/jquery.magnific-popup.js" type="text/javascript"></script>
<link href="/static/myhome/css/magnific-popup.css" rel="stylesheet" type="text/css">


<link type="text/css" rel="stylesheet" href="/static/css/base.css" />
<link type="text/css" rel="stylesheet" href="/static/css/module.css"  />

    <script>
			$(document).ready(function() {
				$('.popup-with-zoom-anim').magnificPopup({
					type: 'inline',
					fixedContentPos: false,
					fixedBgPos: true,
					overflowY: 'auto',
					closeBtnInside: true,
					preloader: false,
					midClick: true,
					removalDelay: 300,
					mainClass: 'my-mfp-zoom-in'
			});
		});

    </script>
</head>
<body>
{% include '../common/header.html' %}
<div class="main">
    <div class="container">
        <div class="Product_top">
            <div class="row content">

             <div class="shopping">



	<div class="shop-group-item">
		<div class="shop-name">

			<div class="coupons"><em>|</em><span>编辑</span><!--<span class="shop-total-amount ShopTotal">0</span>--></div>
		</div>
		<ul>
			{% for v in cartdata %}
			<li>
				<div class="shop-info" cartid="{{ v.id }}" >
					<input type="checkbox" class="check goods-check goodsCheck" style="width:20pxs;position: relative; top:65px;}">
					<div class="shop-info-img"><a href="#"><img src="{{ v.goodsid.pic_url }}" /></a></div>
					<div class="shop-info-text">
						<h4>{{ v.goodsid.goodsname }}</h4>
						<div class="shop-brief"><span>{{ v.goodsid.title }}</span></div>
						<div class="shop-price">
							<div class="shop-pices">￥<b class="price">{{ v.goodsid.price }}</b></div>
							<div class="shop-arithmetic">
								<a href="javascript:;" class="minus" cartid="{{ v.id }}">-</a>
								<span class="num" >{{ v.num }}</span>
								<a href="javascript:;" class="plus" cartid="{{ v.id }}">+</a>
								<a class="delCart" cartid="{{ v.id }}" style="color:red; ">×</a>
							</div>

						</div>
					</div>
				</div>

			</li>
			{% endfor %}
		</ul>
		<div class="shopPrice">本店总计：￥<span class="shop-total-amount ShopTotal">0.00</span></div>
	</div>
</div>

<div class="payment-bar">
	<div class="all-checkbox"><input type="checkbox" class="check goods-check" style="position: relative; top:17px;}" id="AllCheck">全选</div>
	<div class="shop-total">
		<strong>总价：<i class="total" id="AllTotal">0.00</i></strong>
	</div>
	<a href="#" class="settlement">结算</a>
</div>
                <div class="clearfix"></div>

            </div>
        </div>
    </div>
</div>
{% include '../common/footer.html' %}
<script>
		$(function(){
	// 数量减
	$(".minus").click(function() {
		var t = $(this).parent().find('.num');
		t.text(parseInt(t.text()) - 1);
		if (t.text() <= 1) {
			t.text(1);
		}
		TotalPrice();
		var cartid = $(this).attr("cartid")
		$.get('/carts/edit/',{'cartid':cartid,'num':t.text()})
	});
	// 数量加
	$(".plus").click(function() {
		var t = $(this).parent().find('.num');
		t.text(parseInt(t.text()) + 1);
		if (t.text() <= 1) {
			t.text(1);
		}
		TotalPrice();
		var cartid = $(this).attr("cartid")
		$.get('/carts/edit/',{'cartid':cartid,'num':t.text()})
	});
	//删除订单
	$(".delCart").click(function(){
		var cartid = $(this).attr("cartid")
		var a = $(this)
		$.get('/carts/del/',{'cartid':cartid},function(data){

			 if(data['code'] == 0){
			 	alert(data['msg'])
				a.parents('li').remove()
			}

		},'json')

	});
	/******------------分割线-----------------******/
	  // 点击商品按钮
  $(".goodsCheck").click(function() {
    var goods = $(this).closest(".shop-group-item").find(".goodsCheck"); //获取本店铺的所有商品
    var goodsC = $(this).closest(".shop-group-item").find(".goodsCheck:checked"); //获取本店铺所有被选中的商品
    var Shops = $(this).closest(".shop-group-item").find(".shopCheck"); //获取本店铺的全选按钮
    if (goods.length == goodsC.length) { //如果选中的商品等于所有商品
      Shops.prop('checked', true); //店铺全选按钮被选中
      if ($(".shopCheck").length == $(".shopCheck:checked").length) { //如果店铺被选中的数量等于所有店铺的数量
        $("#AllCheck").prop('checked', true); //全选按钮被选中
        TotalPrice();
      } else {
        $("#AllCheck").prop('checked', false); //else全选按钮不被选中
        TotalPrice();
      }
    } else { //如果选中的商品不等于所有商品
      Shops.prop('checked', false); //店铺全选按钮不被选中
      $("#AllCheck").prop('checked', false); //全选按钮也不被选中
      // 计算
      TotalPrice();
      // 计算
    }
  });
  // 点击店铺按钮
  $(".shopCheck").click(function() {
    if ($(this).prop("checked") == true) { //如果店铺按钮被选中
      $(this).parents(".shop-group-item").find(".goods-check").prop('checked', true); //店铺内的所有商品按钮也被选中
      if ($(".shopCheck").length == $(".shopCheck:checked").length) { //如果店铺被选中的数量等于所有店铺的数量
        $("#AllCheck").prop('checked', true); //全选按钮被选中
        TotalPrice();
      } else {
        $("#AllCheck").prop('checked', false); //else全选按钮不被选中
        TotalPrice();
      }
    } else { //如果店铺按钮不被选中
      $(this).parents(".shop-group-item").find(".goods-check").prop('checked', false); //店铺内的所有商品也不被全选
      $("#AllCheck").prop('checked', false); //全选按钮也不被选中
      TotalPrice();
    }
  });
  // 点击全选按钮
  $("#AllCheck").click(function() {
    if ($(this).prop("checked") == true) { //如果全选按钮被选中
      $(".goods-check").prop('checked', true); //所有按钮都被选中
      TotalPrice();
    } else {
      $(".goods-check").prop('checked', false); //else所有按钮不全选
      TotalPrice();
    }
    $(".shopCheck").change(); //执行店铺全选的操作
  });
	//计算
  function TotalPrice() {
    var allprice = 0; //总价
    $(".shop-group-item").each(function() { //循环每个店铺
      var oprice = 0; //店铺总价
      $(this).find(".goodsCheck").each(function() { //循环店铺里面的商品
        if ($(this).is(":checked")) { //如果该商品被选中
          var num = parseInt($(this).parents(".shop-info").find(".num").text()); //得到商品的数量
          var price = parseFloat($(this).parents(".shop-info").find(".price").text()); //得到商品的单价
          var total = price * num; //计算单个商品的总价
          oprice += total; //计算该店铺的总价
        }
        $(this).closest(".shop-group-item").find(".ShopTotal").text(oprice.toFixed(2)); //显示被选中商品的店铺总价
      });
      var oneprice = parseFloat($(this).find(".ShopTotal").text()); //得到每个店铺的总价
      allprice += oneprice; //计算所有店铺的总价
    });
    $("#AllTotal").text(allprice.toFixed(2)); //输出全部总价
  }
});
</script>
</body>
</html>

